import {createStore} from 'vuex'

export default createStore({
    state: {
        user: JSON.parse(localStorage.getItem('user')) || null
    },
    mutations: {
        // 设置用户
        SET_USER(state, user) {
            state.user = user
            localStorage.setItem('user', JSON.stringify(user))
        }
    },
    actions: {
        // 只暴露一个 action 供调用
        setUser({commit}, user) {
            commit('SET_USER', user); //调用  // 设置用户SET_USER方法
        }
    },
    getters: {
        // 获取用户
        user: state => state.user,
        isLoggedIn: state => !!state.user
    }

})
